/**
 * 
 */
package org.lookouster.nextAgadir.pointageBureau.bases;

import java.math.BigInteger;
import java.sql.SQLException;

import org.lookouster.nextAgadir.pointageBureau.outils.BDConnect;
import org.lookouster.nextAgadir.pointageBureau.outils.BDFunctions;
import org.lookouster.nextAgadir.pointageBureau.outils.Commons;
import org.lookouster.nextAgadir.pointageBureau.outils.MyBlowFish;
import org.lookouster.nextAgadir.pointageBureau.outils.functions;
import org.lookouster.nextAgadir.pointageBureau.ui.UICommons;
import org.lookouster.nextAgadir.pointageBureau.dbDESC.*;

/**
 * @author Lookouster
 *
 */
public class Compte {
	
	public Compte(String uname, String pass, int level)
	{
		login=uname;
		password=pass;
		niveau=level;
	}
	
	public Compte(int id, String uname, String pass, int level)
	{
		login=uname;
		password=pass;
		niveau=level;
		idCompte=id;
	}
	
	int idCompte,niveau;
	String login, password;
	public int getIdCompte() {
		return idCompte;
	}
	public void setIdCompte(int idCompte) {
		this.idCompte = idCompte;
	}
	public int getNiveau() {
		return niveau;
	}
	public void setNiveau(int niveau) {
		this.niveau = niveau;
	}
	public String getLogin() {
		return login;
	}
	public void setLogin(String login) {
		this.login = login;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	
	public boolean saveDB(BDConnect bdc)
	{
		MyBlowFish bf = new MyBlowFish();
    	bf.setSecretKey(UICommons.DEF_KEY);
    	
    	byte[] cryptPass = bf.crypt(password); 
		String sql="insert into "+Tables.DB_TABLE_CPTES+" ("+CPTES.CHP_LOGIN+","+
		CPTES.CHP_PASS+","+CPTES.CHP_LEVEL+") values('"+login+"','"+new BigInteger(cryptPass)+
		"',"+niveau+")";
		try {
			bdc.update(sql);
			idCompte=BDFunctions.getLastID(Tables.DB_TABLE_CPTES, CPTES.CHP_ID, bdc);
			return true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			//e.printStackTrace();
			functions.affiche("pb creating compte: "+e.getMessage());
			return false;
		}
	}
	
	public boolean updatePassDB(BDConnect bdc, String newP)
	{
		System.out.println("passi");
		MyBlowFish bf = new MyBlowFish();
    	bf.setSecretKey(UICommons.DEF_KEY);
    	
    	byte[] cryptPass = bf.crypt(newP);
    	String sql="update "+Tables.DB_TABLE_CPTES+" set "+CPTES.CHP_PASS+"='"
    	+new BigInteger(cryptPass)+
    	"' where "+CPTES.CHP_ID+"="+idCompte;
    	//System.out.println(sql);
    	try {
			bdc.update(sql);
			password=newP;
			return true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			//e.printStackTrace();
			functions.affiche("pb updating compte: "+e.getMessage());
			return false;
		}
	}
	
	public void updateCompte()
	{
		try {
				BDConnect bdc=new BDConnect();
				//functions.affiche(password+"----"+Commons.defPass);
				
					
				String sql="update "+Tables.DB_TABLE_CPTES+" set "+
					CPTES.CHP_LEVEL+"="+niveau+", "+CPTES.CHP_LOGIN+"='"+login+"'"
					+" where "+CPTES.CHP_ID+"="+idCompte;
				if(!this.getPassword().equals(Commons.defPass))
					updatePassDB(bdc,password);
				//System.out.println(sql);
				bdc.update(sql);
				bdc.shutdown();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	

}
